﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FileSystemAnalysis.Code
{
    internal class Model
    {
        private DataTable data;

        private object mutex = new object();

        public Model()
        {
            data = new DataTable("Data");
            data.Columns.Add("path", typeof(string));
            data.Columns.Add("SHA256", typeof(string));
            data.Constraints.Add("Primary", data.Columns["path"], true);
        }

        private bool indexRunning = false;
        public bool Indexing
        {
            get
            {
                lock (mutex)
                {
                    return indexRunning;
                }
            }
            set
            {
                lock (mutex)
                {
                    indexRunning = value;
                }
            }
        }

        private bool running = false;
        
        public bool Running
        {
            get
            {
                lock (mutex)
                {
                    return running;
                }
            }
            set
            {
                lock (mutex)
                {
                    running = value;
                }
            }
        }

        public int Count
        {
            get
            {
                lock (mutex)
                {
                    return data.Rows.Count;
                }
            }
        }

        private ulong filesIndexed = 0L;
        public ulong FilesIndexed {
            get
            {
                lock (mutex)
                {
                    return filesIndexed;
                }
            }
            set
            {
                lock (mutex)
                {
                    filesIndexed = value;
                }
            }
        }

        private ulong filesShaed = 0L;
        public ulong FilesShaed
        {
            get
            {
                lock (mutex)
                {
                    return filesShaed;
                }
            }
            set
            {
                lock (mutex)
                {
                    filesShaed = value;
                }
            }
        }

        public void addRow(string path, string sha256)
        {
            lock (mutex)
            {
                DataRow dr = data.NewRow();
                dr["path"] = path;
                dr["SHA256"] = sha256;
                data.Rows.Add(dr);
            }
        }

        public DataTable Data
        {
            get
            {
                lock (mutex)
                {
                    if (running)
                        return null;
                    else
                        return data;
                }
            }
        }

        private bool shaing = false;
        public bool Shaing {
            get {
                lock (mutex)
                {
                    return shaing;
                }
            }
            set {
                lock (mutex)
                {
                    shaing = value;
                }
            }
        }

        private ulong sentForSha = 0L;
        public ulong SentForSha
        {
            get
            {
                lock (mutex)
                {
                    return sentForSha;
                }
            }
            set
            {
                lock (mutex)
                {
                    sentForSha = value;
                }
            }
        }
    }
}
